package com.example.neusoft.zyfypt_wk.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.zyfypt_wk.R;
import com.example.neusoft.zyfypt_wk.activity.ViewArticleActivity;
import com.example.neusoft.zyfypt_wk.activity.ViewTwareActivity;
import com.example.neusoft.zyfypt_wk.bean.ArticleBean;
import com.example.neusoft.zyfypt_wk.bean.TwareBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Neusoft on 2017/4/13.
 */

public class TwareAdapter extends RecyclerView.Adapter {
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态加载布局
    private List<TwareBean>  list;//保存要显示的数据

/*    public interface onItemClickListener{
        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }*/
/*
public interface OnItemClickListener {
    void onItemClick(View view, int position);

    void onItemLongClick(View view, int position);
}
    private OnItemClickListener onItemClickListener=null;
*/




    //1自定义：构造方法，传进上下文
    public TwareAdapter(Context context) {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    //2自定义：获取数据
    public void setList(List<TwareBean> list)
    {
        this.list=list;
    }
    //3自定义：ViewHolder子类
    public class ViewHolder extends RecyclerView.ViewHolder
    {   private ImageView imageView;
        private TextView tvtitle,tvdescrrpt,tvtime;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView=(ImageView)itemView.findViewById(R.id.imageView);
            tvtitle=(TextView)itemView.findViewById(R.id.textView);
            tvdescrrpt=(TextView)itemView.findViewById(R.id.textView2);
            tvtime=(TextView)itemView.findViewById(R.id.textView3);
        }
    }
    //4重写：生成Item的View
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item,parent,false);
        return new ViewHolder(v);
    }
    //5重写：给ViewHolder中的控件填充值，设置监听
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        TwareBean  bean=list.get(position);
        if(bean==null)
            return;
        final ViewHolder  viewHolder=(ViewHolder)holder;
        //viewHolder.imageView.setImageResource(articleBean.getImgid());
        viewHolder.tvtitle.setText(bean.getName());
        viewHolder.tvdescrrpt.setText("描述："+bean.getDescription());
        viewHolder.tvtime.setText(bean.getUpdate_time());
        //异步加载图片
        Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"
                        +bean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);

       // if (onItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //获取当前条目信息，如id
                    int id = list.get(position).getId();
                    Toast.makeText(context, "" + id, Toast.LENGTH_SHORT).show();
                    //实际中，点击后打开新窗口做其他操作
                    Intent intent=new Intent(context, ViewTwareActivity.class);
                    intent.putExtra("resid",list.get(position).getId());
                    intent.putExtra("userid",list.get(position).getUserid());
                    intent.putExtra("pdfattach",list.get(position).getPdfattach());
                    intent.putExtra("name",list.get(position).getName());
                    context.startActivity(intent);

                   // int position = viewHolder.getLayoutPosition();
                    //onItemClickListener.onItemClick(viewHolder.itemView, position);
                }
            });
        //}
    }
    //共有多少条目
    @Override
    public int getItemCount() {
        if(list==null)  return 0;
        else
            return list.size();
    }

   /* public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }*/
}
